package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 游记推荐controller
 */

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {

    @Autowired
    private ITravelCommendService travelCommentService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IMessageService messageService;

    /**
     * 展示游记推荐信息 按照推荐时间表倒序排列
     * @param qo : 关键字查询/推荐类型查询/分页+排序
     * @return
     */
    @RequestMapping("/list")
    public PageInfo list(@ModelAttribute("qo") TravelCommentQueryObject qo) {
        qo.setOrderBy("schedule DESC");
        return travelCommentService.query(qo);
    }

    /**
     * 新增推荐游记
     * @param travelCommend : 游记推荐对象
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(TravelCommend travelCommend) {
        JsonResult jsonResult = new JsonResult();

        try {
            if (travelCommend.getId() == null) {
                //查询当前游记

                //在发布或者拒绝的时候,存入消息内容,用于发送给用户
                Message message=new Message();
                //作者id
                //查询当前游记的信息
                if(travelCommend.getTravel()!=null){
                    Travel currentTravel = travelService.getById(travelCommend.getTravel().getId());
                    Travel currentTravel2 = travelService.get(currentTravel.getId());
                    message.setReceiver(currentTravel2.getAuthor());
                }


                //系统通知类型
                message.setType(4);
                //设置消息未读状态
                message.setState(0);
                if(travelCommend.getType()==2||travelCommend.getType()==3||travelCommend.getType()==1){
                    message.setContent("亲,您写的游记被叩丁狼官方推荐了,真棒~");
                }

                //游记
                message.setTravel(travelCommend.getTravel());
                //发送时间
                message.setSendTime(new Date());

                //保存消息数据到数据库中
                messageService.save(message);
                travelCommentService.save(travelCommend);
            }else{
                travelCommentService.update(travelCommend);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }

}
